const util = require('../../../utils/util.js')
const ajax = require('../../../utils/ajax/ajax.js')
Page({
    data: {
        userName: '',
        title: '',
        address: '',
        projectImg: '',
        formData: {
            userName: '',
            userCard: '',
            userPhone: '',
            recommendCode: ''
        },
        fileList: [],
        isDisabled: true,
        proId: ''
    },
    onLoad: function (options) {
        const {
            address,
            projectImg,
            projectName,
            proId
        } = options
        this.showProjectDeatil(address, projectImg, projectName, proId)
    },
    // 显示 header 项目详情
    showProjectDeatil(address, projectImg, projectName, proId) {
        this.setData({
            title: projectName,
            address: address,
            projectImg: projectImg,
            proId: proId
        })
    },
    deleteImg(e) {
        const delIndex = e.detail.index
        const fileList = this.data.fileList
        fileList.splice(delIndex, 1)
        this.setData({
            fileList
        })
    },
    afterRead(e) {
        const that = this
        const url = e.detail.file.url
        util.showLoading('图片上传中')
        util.uploadImg({
            url,
            success(res) {
                const fileList = that.data.fileList
                fileList.push({
                    url: res.data
                })
                that.setData({
                    fileList
                })
                util.hideLoading()
            },
            fail() {
                util.toast('图片上传失败')
            },
            complete() {
                util.hideLoading()
            }
        })
    },
    inputChange(e) {
        const dataset = e.currentTarget.dataset
        const value = e.detail.value
        const formData = this.data.formData
        formData[dataset.label] = value
        this.setData({
            formData,
            isDisabled: false
        })
    },
    commit() {
        const {
            userName,
            userCard,
            userPhone,
            recommendCode
        } = this.data.formData
        const proId = this.data.proId
        const fileList = this.data.fileList

        const regCard = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
        const regPhone = /^1[3456789]\d{9}$/;

        if (!userName) {
            util.toast('请输入姓名')
            return
        }
        if (!regCard.test(userCard)) {
            util.toast('身份证号有误')
            return
        }
        if (!regPhone.test(userPhone)) {
            util.toast('手机号有误')
            return
        }

        if (fileList.length === 0) {
            ajax.post({
                url: '/wx/applyProject',
                data: {
                    user_name: userName,
                    id_card: userCard,
                    phone: userPhone,
                    project_id: proId,
                    be_rec_code: recommendCode || ''
                },
                success(res) {
                    const id = res.data.id
                    // 显示补充凭证按钮 去补充凭证
                    const isVoucher = '无凭证'
                    wx.navigateTo({
                        url: `/pages/Index/listApplySuccess/listApplySuccess?proId=${id}&isVoucher=${isVoucher}`
                    })
                },
                fail(err) {
                    util.toast(err.msg)
                }
            }) 
            return
        }

        let imgs = '';
        for (let i = 0; i < fileList.length; i++) {
            const json = fileList[i].url;
            imgs += json + ',';
        }
        const uploadImg = imgs.substring(0, imgs.lastIndexOf(','))
        console.log(uploadImg);
        ajax.post({
            url: '/wx/applyProject',
            data: {
                user_name: userName,
                id_card: userCard,
                phone: userPhone,
                project_id: proId,
                verify_url: uploadImg,
                be_rec_code: recommendCode || ''
            },
            success() {
                wx.navigateTo({
                    url: '/pages/Index/listApplySuccess/listApplySuccess?proId=' + proId, 
                })
            },
            fail(err) {
                util.toast(err.msg)
            }
        }) 
    }
})